package home

import (
    "fmt"
	"net/http"
	"html/template"
)


var (
	getTemplate = template.Must(template.ParseFiles("get.html"))
	errorTemplate  = template.Must(template.ParseFiles("error.html"))
)

// Because App Engine owns main and starts the HTTP service,
// we do our setup during initialization.
func init() {
	http.HandleFunc("/", errorHandler(get))
	/*
	http.HandleFunc("/edit", errorHandler(edit))
	http.HandleFunc("/img", errorHandler(img))
	http.HandleFunc("/share", errorHandler(share))
	http.HandleFunc("/post", errorHandler(post))
	editTemplate = template.New(nil)
	editTemplate.SetDelims("{{{", "}}}")
	if err := editTemplate.ParseFile("edit.html"); err != nil {
		panic("can't parse edit.html: " + err.String())
	}
	*/
}

// edit is the HTTP handler for editing images; it handles "/edit".
func get(w http.ResponseWriter, r *http.Request) {
	getTemplate.Execute(w, nil) //Execute(w, r.FormValue("id"))
}


// errorHandler wraps the argument handler with an error-catcher that
// returns a 500 HTTP error if the request fails (calls check with err non-nil).
func errorHandler(fn http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if err, ok := recover().(error); ok {
				w.WriteHeader(http.StatusInternalServerError)
				errorTemplate.Execute(w, err)
			}
		}()
		fn(w, r)
	}
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello, world!")
}